#FIXEST
setwd("C:/Users/au481583/OneDrive - Aarhus universitet/Election pledges/Tekst/Udkast/Puzzles/Puzzle 2/replication/")

library(fixest)
library(modelsummary)
library(flextable)
library(dplyr)

citation("fixest")

data <- read.csv("data/analysis.csv")

data_complete <- na.omit(data[, c("sh_i_char_commitment", "PositionChangeType1", "cabinet_party", "LSaliency", "rile_index",  "issue_num", "pf_common_id")])

model <- feols(sh_i_char_commitment ~ factor(PositionChangeType1) + cabinet_party + LSaliency + rile_index | issue_num + pf_common_id, data = data_complete)

model
r2(model)


#Table D12
modelsummary(model, output = "flextable", file = "Table D12.docx")

table <- modelsummary(
  model,
  coef_rename = c(
    "LSaliency" = "Saliency (previous)",
    "rile_index" = "Ideology (rile)",
    "cabinet_party" = "Government party (ref = opposition party)",
    "factor(PositionChangeType1)1" = "Type of change: Ideological crystallization (ref = no change)",
    "factor(PositionChangeType1)2" = "Ideological dilution"
  ),
  coef_omit = NULL,
  stars = c('*' = .05, '**' = .01, '***' = .001),
  gof_omit = "AIC|R2 Adj.|R2 Within|R2 Within Adj.|BIC|RMSE|Std.Errors|FE: issue_num|FE: pf_common_id",
  output = "flextable",
  notes = list(
    "Notes: *p<0.05, **p<0.01, ***p<0.001. Coefficients are estimated based on a pooled OLS-regression model with issue and party fixed effects. Standard errors are in parentheses. Main independent variable is type of change (ideological dilution is when a party becomes ideologically more mixed on an issue from one election to the next, ideological crystallization is when a party becomes ideologically more clear on an issue, and no change is when a party is ideologically consistent across elections - within a certain threshold)."
  ))

table
flextable::save_as_docx(table, path = "output/Table D12.docx")